package com.wan.activity;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wan.activity.util.EditTextFocusLintener;
import com.wan.commonsdk.Conet;
import com.wan.commonsdk.HttpUtils;
import com.wan.commonsdk.WanCommonSdk;
import com.wan.commonsdk.WanServerResposeCode;

import net.sourceforge.simcpux.Util;
import java.util.HashMap;
//------------------------------------------------------------------------
// 密码修改
//
//
//------------------------------------------------------------------------
public class changePassword extends BaseActivity implements View.OnClickListener {
    public static Activity activity = null;
    private Thread mThreadp;
    private Button btn_change;
    private TextView change_back_btn;
    private final int REGESTER_SUCCESS = WanServerResposeCode.WAN_REGESTER_SUCCESS;
    private final int REGESTER_FAILURE = WanServerResposeCode.WAN_REGESTER_FAILURE;
    private final int NETWORK_ERROR = 10086;
    private EditText password,newpassword,newpasswordCheck;
    private TextView finish_text;
    private String _password,_newpassword,status;
    private String commend;
    
    private Button chang_un_edclose,chang_newpw_edclose,chang_newpwcheck_edclose;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (WanCommonSdk.screen_orientation == WanCommonSdk.SCREEN_ORIENTATION_PORTRAIT)
        {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        else
        {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        super.onCreate(savedInstanceState);
        setContentView(Util.getResID("change_password", "layout"));
        activity                =   this;
        this.btn_change        =   (Button) findViewById( Util.getResID("btn_change", "id"));
        btn_change.setOnClickListener(this);
        change_back_btn = (TextView)findViewById(Util.getResID("change_back_btn", "id"));
        change_back_btn.setOnClickListener(this);
        this.finish_text        =   (TextView) findViewById(Util.getResID("finish_text", "id"));
        this.password           =   (EditText)findViewById(Util.getResID("password", "id"));
        this.newpassword        =   (EditText) findViewById(Util.getResID("newpassword", "id"));
        this.newpasswordCheck   =   (EditText) findViewById(Util.getResID("newpassword_check", "id"));
        chang_un_edclose = (Button) findViewById(Util.getResID("chang_un_edclose", "id"));
        chang_newpw_edclose = (Button) findViewById(Util.getResID("chang_newpw_edclose", "id"));
        
        chang_newpwcheck_edclose = (Button) findViewById(Util.getResID("chang_newpwcheck_edclose", "id"));
        
        password.setOnFocusChangeListener(new EditTextFocusLintener(password, chang_un_edclose));
        newpassword.setOnFocusChangeListener(new EditTextFocusLintener(newpassword, chang_newpw_edclose));
        newpasswordCheck.setOnFocusChangeListener(new EditTextFocusLintener(newpasswordCheck, chang_newpwcheck_edclose));
    }

    //------------------------------------------------------------------------
    //
    // 消息处理
    //
    //------------------------------------------------------------------------
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {//�˷�����ui�߳�����
            switch (msg.what) {
                case REGESTER_SUCCESS:
                    hideLoading();
                    Conet.saveuserInfo();
                    btn_change.setVisibility(View.GONE);
                    password.setVisibility(View.GONE);
                    newpassword.setVisibility(View.GONE);
                    newpasswordCheck.setVisibility(View.GONE);
                    finish_text.setText("密码修改成功");
                    break;
                case NETWORK_ERROR:
                    hideLoading();

                    mThreadp = null;
                    Toast.makeText(getApplicationContext(), "网络出现问题，请重试",
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
                    hideLoading();
//                    showToast(status);
                    Toast.makeText(getApplicationContext(), "原密码不正确",
                            Toast.LENGTH_SHORT).show();
                    mThreadp = null;
                    break;

            }
        }
    };
    //------------------------------------------------------------------------
    //
    // 线程处理
    //
    //------------------------------------------------------------------------
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            String userid = Conet.mUserInfo.getUser_id();
            String username = Conet.mUserInfo.getUser_name();
            HashMap<String, String> result = HttpUtils.HttpUpdatePasswrd(username, _password, _newpassword,userid,"1","");
           Util.LogUtil("HttpUpdatePasswrd------result:"+result);
            if (result == null)
            {
                mHandler.obtainMessage(NETWORK_ERROR).sendToTarget();
                return;
            }
            else if(result.get("status").equals(String.valueOf(REGESTER_SUCCESS)))
            {
            	Conet.mUserInfo.setUser_password(_newpassword);
                mHandler.obtainMessage(REGESTER_SUCCESS, Conet.mUserInfo).sendToTarget();

                return;
            } else if (result.get("status").equals(String.valueOf(WanServerResposeCode.WAN_HAS_ACCOUNT)))
            {
                commend = result.get("commend");
            }
            status	=	result.get("status").toString();
            mHandler.obtainMessage(Integer.parseInt(result.get("status"))).sendToTarget();
        }
    };

    //------------------------------------------------------------------------
    //事件处理
    //
    //------------------------------------------------------------------------
    @Override
    public void onClick(View v) {
        if (v.getId() == Util.getResID("btn_change", "id")) {
        	
            if(!checkUserInfo())
                return;
            _password   =   this.password.getText().toString();
            _newpassword=   this.newpassword.getText().toString();
            showLoading("修改中");
            
            mThreadp = new Thread(runnable);
            mThreadp.start();
		}else if(v.getId() == Util.getResID("change_back_btn", "id")){
			
			finish();
		}
    }

    //------------------------------------------------------------------------
    //
    // 检查输入信息
    //
    //------------------------------------------------------------------------
    private Boolean checkUserInfo()
    {
        int un_length = this.password.getText().toString().length();
        int pw_length = this.newpassword.getText().toString().length();
        int npw_length = this.newpasswordCheck.getText().toString().length();

        String npw      =   this.newpassword.getText().toString();
        String npwc     =   this.newpasswordCheck.getText().toString();
        if (un_length <6 || un_length>32)
        {
            Toast.makeText(getApplicationContext(), this.getString(Util.getResID("password_empty_hint", "string")),
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        if (pw_length <6 || pw_length>32)
        {
            Toast.makeText(getApplicationContext(), this.getString(Util.getResID("newpassword_empty_hint", "string")),
                    Toast.LENGTH_SHORT).show();   
            return false;
        }
        if (npw_length == 0)
        {
            Toast.makeText(getApplicationContext(), this.getString(Util.getResID("newpasswordc_empty_hint", "string")),
                    Toast.LENGTH_SHORT).show(); 
            return false;
        }

        if (!npw.equals(npwc) ){
            Toast.makeText(getApplicationContext(), this.getString( Util.getResID("confirmpw_error_hint", "string")),
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

}
